﻿using DevExpress.Utils;
using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.SIESystemManager.BLL;
using Lamex.SIESystemManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.SIESystemManager.UI
{
    public partial class FrmBIEProcessOperation : frmBaseDataForm
    {
        public FrmBIEProcessOperation()
        {
            InitializeComponent();
            InitializeForm();
            this.Load += new EventHandler(FrmBIEProcessOperation_Load);
        }
        public string IsEdit = "";
        public string KeysName = "";
        private void FrmBIEProcessOperation_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        protected override void InitializeForm()
        {
            _SummaryView = new DevGridView(gridView2);
            gridView2.DoubleClick += new EventHandler(OnGridViewDoubleClick); //主表DoubleClict
            frmGridViewCust.RegisterGrid(gridView2);
            DevStyle.SetGridControlLayout(gcView, false);//表格设置   

            DevStyle.SetSummaryGridViewLayout(gridView2);
            DevStyle.SetDetailGridViewLayout(gridView2);
            ShowSummary(); //下载显示数据.
            tpSummary = xtraTabPage1;
            tcBusiness = xtraTabControl1;
            tpSummary.PageVisible = true;
            //tpDetail.PageVisible = false;
            tcBusiness.ShowTabHeader = DefaultBoolean.True;
            tcBusiness.SelectedTabPage = tpSummary;
            //ShowSummaryPage(true); //一切初始化完毕後显示SummaryPage        
        }
        private void ShowSummary()
        {
            try
            {
                //
                //ucLookUpEdit1.ValueMember = "UserName";
                //ucLookUpEdit1.DisPlayMember = "UserName";
                //ucLookUpEdit1.DataSource = dt;
                ////ucLookUpEdit1.UcPopulateColumns();

                ////ucLookUpEdit1.Item[1].Caption = "用户名称";
                //////ucLookUpEdit1.ForceInitialize();
                ////ucLookUpEdit1.UcItemIndex = 0;
                //DoBindingSummaryGrid(dt); //绑定主表的Grid
                //ShowSummaryPage(true); //显示Summary页面. 
                xtraTabPage2.PageEnabled = false;
                xtraTabPage1.PageEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select * from B_IE_Process_Operation where 1=1 ";
            if (!string.IsNullOrEmpty(txtNUMBER.Text))
            {
                sqlWhere += " and NUMBER like '%" + txtNUMBER.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtOPERATIONAL_NAME.Text))
            {
                sqlWhere += " and txtOPERATIONAL_NAME like '%" + txtOPERATIONAL_NAME.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtTYPE.Text))
            {
                sqlWhere += " and TYPE like '%" + txtTYPE.Text + "%'";
            }

            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<B_IE_Process_Operation>.Instance.GetDataTableBySql(sql);
            DoBindingSummaryGrid(dt);
            base.DoView(sender);

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Process_Operation", "LamexMESManager")) return;
            #endregion
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            IsEdit = "新增";
            xtraTabPage2.Tag = "新增";
            base.DoAdd(sender);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Process_Operation", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除要修改的数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            KeysName = dr["IDNO"].ToString();
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;

            IsEdit = "编辑";
            xtraTabPage2.Tag = "编辑";

            //***********************加载数据****************************//

            txtEditNUMBER.Text = dr["NUMBER"].ToString();
            txtEditDESCRIPTION_NUMBER.Text = dr["DESCRIPTION_NUMBER"].ToString();
            txtEditUnit.Text = dr["Unit"].ToString();
            txtEditOPERATIONAL_NAME.Text = dr["OPERATIONAL_NAME"].ToString();
            txtEditTYPE.Text = dr["TYPE"].ToString();
            txtEditDescription.Text = dr["Description"].ToString();

            base.DoEdit(sender);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Process_Operation", "LamexMESManager")) return;
            #endregion
            if (txtEditNUMBER.Alter()) return;
            if (IsEdit == "新增")
            {
                B_IE_Process_OperationInfo model = new B_IE_Process_OperationInfo();
                model.NUMBER = txtEditNUMBER.Text;
                model.DESCRIPTION_NUMBER = txtEditDESCRIPTION_NUMBER.Text;
                model.DESCRIPTION = txtEditDescription.Text;
                model.OPERATIONAL_NAME = txtEditOPERATIONAL_NAME.Text;
                model.TYPE = txtEditTYPE.Text;
                model.UNIT = txtEditUnit.Text;

                if (BaseFactory<B_IE_Process_Operation>.Instance.Insert(model))
                {
                    MessageBox.Show("保存存成功！");

                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }

            }
            if (IsEdit == "编辑")
            {
                B_IE_Process_OperationInfo model = new B_IE_Process_OperationInfo();
                model.IDNO = int.Parse(KeysName);
                model.NUMBER = txtEditNUMBER.Text;
                model.DESCRIPTION_NUMBER = txtEditDESCRIPTION_NUMBER.Text;
                model.DESCRIPTION = txtEditDescription.Text;
                model.OPERATIONAL_NAME = txtEditOPERATIONAL_NAME.Text;
                model.TYPE = txtEditTYPE.Text;
                model.UNIT = txtEditUnit.Text;
                if (BaseFactory<B_IE_Process_Operation>.Instance.Update(model))
                {
                    MessageBox.Show("保存存成功！");

                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }
            }

            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            base.DoSave(sender);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        public override void DoDelete(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Process_Operation", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<B_IE_Process_Operation>.Instance.Delete(dr["IDNO"].ToString()))
                {
                    MessageBox.Show("【数据删除成功！】");

                }
                else
                {
                    MessageBox.Show("【数据删除失败！】");

                }
            }
            DoView(null);
            base.DoDelete(sender);
        }
        public override void DoImport(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Process_Operation", "LamexMESManager")) return;
            #endregion
            FrmImport frm = new FrmImport("B_IE_Process_Operation");
            frm.ShowDialog();
            base.DoImport(sender);
        }

    }
}
